<template>
	<view style="min-height: 800px;">
		<scroll-view id="nav-bar" scroll-x class="nav bg-black">
			<view class="flex text-center">
				<view class="cu-item flex-sub" :id="'tab'+index" v-for="(item,index) in tabBars" :key="item.id" :class="index==tabCurrentIndex?'text-white cur':''"
				 @tap="changeTab(index)">
					{{item.name}}
				</view>
			</view>
		</scroll-view>
		<!-- 下拉刷新组件 -->
		<!-- <mix-pulldown-refresh ref="mixPulldownRefresh" class="panel-content" :top="90" @refresh="onPulldownReresh"
		 @setEnableScroll="setEnableScroll"> -->
		<!-- 内容部分 -->
		<swiper id="swiper" class="swiper-box" :duration="300" :current="tabCurrentIndex" @change="changeTab">
			<swiper-item v-for="tabItem in tabBars" :key="tabItem.id">
				<scroll-view class="panel-scroll-box" scroll-y="true" @scrolltolower="loadMore" style="margin-top: 5px;">
					<!-- 委托列表 -->
					<view class="cu-list grid col-2 no-border card-menu " style="margin-top: 8px;" v-for="(item, index) in tabItem.newsList" :key="index">
						<view class="cu-item text-left">
							<view class="text-xxl nowrap">{{item.total}}{{item.price}}</view>
							<text class="text-df nowrap">{{item.memo}}</text>
						</view>
						<view class="cu-item text-right">
							<view>
								<text class="text-xs">{{item.addtime_text}}</text>
							</view>
						</view>
						<!-- <view class="cu-item text-left"> 
							<view>
								<view class="text-df row">数量</view>
								<view class="text-df row text-gray"></view>
							</view>
							<view>
								<view class="text-df row">单价</view>
								<view class="text-df row text-gray"></view>
							</view>
						</view>
						<view class="cu-item text-right">
							<view>
								<view class="text-df row">{{item.number}}</view>
								<view class="text-df row text-gray">{{item.ptype1_text}}</view>
							</view>
							<view>
								<view class="text-df row">{{item.price}}</view>
								<view class="text-df row text-gray">CNY</view>
							</view> 
						</view> -->

					</view>

					<!-- 上滑加载更多组件 -->
					<mix-load-more :status="tabItem.loadMoreStatus"></mix-load-more>
				</scroll-view>
			</swiper-item>
		</swiper>
		<!-- </mix-pulldown-refresh> -->
	</view>
</template>

<script>
	import {
		mapState,
		mapActions
	} from 'vuex'
	// import mixPulldownRefresh from '@/components/mix-pulldown-refresh/mix-pulldown-refresh';
	import mixLoadMore from '@/components/mix-load-more/mix-load-more';
	import countdownTime from '@/components/countdown-timer.vue'
	// import json from '@/json'
	let windowWidth = 0,
		scrollTimer = false,
		tabBar;
	export default {
		components: {
			// mixPulldownRefresh,
			mixLoadMore,
			countdownTime
		},
		props: {
			wall_index: {
				type: Number,
				default: () => {
					return 0
				}
			}
		},
		data() {
			return {
				tabCurrentIndex: 0, //当前选项卡索引
				scrollLeft: 0, //顶部选项卡左滑距离
				enableScroll: true,
				tabBars: [],
				category_id: 0,
				TabCur: 0, //当前选项卡索引
			}
		},
		computed: {
			...mapState({
				userinfo: state => state.user.userinfo,
				myconfig: state => state.user.myconfig
			}),
		},
		mounted() {
			console.log(this.wall_index)
			// 获取屏幕宽度
			windowWidth = uni.getSystemInfoSync().windowWidth;
			this.loadTabbars();
			if(this.wall_index>0){
				setTimeout(()=>{
					this.changeTab(this.wall_index);
				},300)
				
			}
		}, 
		methods: { 
			//获取分类
			loadTabbars() {
				let tabList = [];
				for (let key in this.myconfig.cointype) {
					tabList.push({
						name: this.myconfig.cointype[key],
						newsList: [],
						loadMoreStatus: 0, //加载更多 0加载前，1加载中，2没有更多了
						refreshing: 0,
						page: 1,
						ptype: key
					})
				}

				// let tabList = [{
				// 		name: '进行中',
				// 		id: '1',
				// 		status: '0,1'
				// 	},
				// 	{
				// 		name: '已完成',
				// 		id: '2',
				// 		status: '2'
				// 	},
				// 	{
				// 		name: '已取消',
				// 		id: '3',
				// 		status: '3'
				// 	},
				// 	{
				// 		name: '投诉中',
				// 		id: '4',
				// 		status: '4'
				// 	},
				// ];
				// tabList.forEach(item => {
				// 	item.newsList = [];
				// 	item.loadMoreStatus = 0; //加载更多 0加载前，1加载中，2没有更多了
				// 	item.refreshing = 0;
				// 	item.page = 1;
				// })
				this.tabBars = tabList;
				console.log(this.tabBars)
				this.loadNewsList('add');
			},
			//下拉刷新
			onPulldownReresh() {
				this.loadNewsList('refresh');
			},
			//上滑加载
			loadMore() {
				this.loadNewsList('add');
			},
			//设置scroll-view是否允许滚动，在小程序里下拉刷新时避免列表可以滑动
			setEnableScroll(enable) {
				if (this.enableScroll !== enable) {
					this.enableScroll = enable;
				}
			},
			//列表
			loadNewsList(type) {
				let tabItem = this.tabBars[this.tabCurrentIndex];
				console.log(type)
				//type add 加载更多 refresh下拉刷新
				if (type === 'add') {
					if (tabItem.loadMoreStatus === 2) {
						return;
					}
					tabItem.loadMoreStatus = 1;
				}
				// #ifdef APP-PLUS
				else if (type === 'refresh') {
					tabItem.refreshing = true;
				}
				// #endif

				if (type === 'refresh') {
					tabItem.page = 1;
				}
				// 请求数据
				let send = {
					ptype: tabItem.ptype,
					type: '',
					page: tabItem.page
				};
				this.$http.post('user.caiwu', send).then(res => {
					let list = res.data.data;
					if (type === 'refresh') {
						tabItem.newsList = []; //刷新前清空数组
						//下拉刷新 关闭刷新动画
						this.$refs.mixPulldownRefresh && this.$refs.mixPulldownRefresh.endPulldownRefresh();
						// #ifdef APP-PLUS
						tabItem.refreshing = false;
						// #endif
						tabItem.loadMoreStatus = 0;
					}
					list.forEach(item => {
						// item.id = parseInt(Math.random() * 10000);
						tabItem.newsList.push(item);
					})

					tabItem.loadMoreStatus = res.data.current_page >= res.data.last_page ? 2 : 0;
					tabItem.page += 1;
				})

			}, 
			//tab切换
			async changeTab(e) {

				if (scrollTimer) {
					//多次切换只执行最后一次
					clearTimeout(scrollTimer);
					scrollTimer = false;
				}
				let index = e;
				//e=number为点击切换，e=object为swiper滑动切换
				if (typeof e === 'object') {
					index = e.detail.current
				}
				if (typeof tabBar !== 'object') {
					tabBar = await this.getElSize("nav-bar")
				}
				//计算宽度相关
				let tabBarScrollLeft = tabBar.scrollLeft;
				let width = 0;
				let nowWidth = 0;
				//获取可滑动总宽度
				for (let i = 0; i <= index; i++) {
					let result = await this.getElSize('tab' + i);
					width += result.width;
					if (i === index) {
						nowWidth = result.width;
					}
				}
				if (typeof e === 'number') {
					//点击切换时先切换再滚动tabbar，避免同时切换视觉错位
					this.tabCurrentIndex = index;
				}
				//延迟300ms,等待swiper动画结束再修改tabbar
				scrollTimer = setTimeout(() => {
					if (width - nowWidth / 2 > windowWidth / 2) {
						//如果当前项越过中心点，将其放在屏幕中心
						this.scrollLeft = width - nowWidth / 2 - windowWidth / 2;
					} else {
						this.scrollLeft = 0;
					}
					if (typeof e === 'object') {
						this.tabCurrentIndex = index;
					}
					this.tabCurrentIndex = index;

					//第一次切换tab，动画结束后需要加载数据
					let tabItem = this.tabBars[this.tabCurrentIndex];
					if (this.tabCurrentIndex !== 0 && tabItem.loaded !== true) {
						this.loadNewsList('add');
						tabItem.loaded = true;
					}
				}, 300)

			},
			//获得元素的size
			getElSize(id) {
				return new Promise((res, rej) => {
					let el = uni.createSelectorQuery().select('#' + id);
					el.fields({
						size: true,
						scrollOffset: true,
						rect: true
					}, (data) => {
						res(data);
					}).exec();
				});
			},
			// tabSelect(e) {
			// 	this.tabCurrentIndex = e.currentTarget.dataset.id;
			// 	this.scrollLeft = (e.currentTarget.dataset.id - 1) * 60
			// }
		}
	}
</script>

<style scoped>
	.cu-item .text-gray {
		width: 45px;
		display: inline-block;
	}

	.row {
		display: inline;
	}

	.bai {
		padding: 6px 12px;
		border-radius: 8px;
	}

	.nowrap {
		white-space: nowrap;
	}

	.cu-list {
		margin-top: 2px;
	}

	.cu-list.grid.no-border {
		padding: 3px 17px;
	}

	.nav .cu-item {
		border: none;
	}

	.nav .cu-item.cur {
		border-bottom: 1px solid;
	}

	.swiper-box {
		min-height: 800px;
	}

	.panel-scroll-box {
		height: 100%;

		.panel-item {
			background: #fff;
			padding: 30px 0;
			border-bottom: 2px solid #000;
		}
	}
</style>
